package com.hdoit.ares.web.insu.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.insu.model.InsurancePeople;
import com.hdoit.ares.logical.insu.service.IInsurancePeopleService;
import com.hdoit.ares.logical.viewmodel.insu.InsurancePeopleSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author caochengmeng
 * @email
 * @createtime 2017年11月04日 10:22:37
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/insu/people/")
public class InsurancePeopleController extends BaseController {
	@Override
	public String getRoutePath() {
		return "areas/admin/insu/people/";
	}

	private IInsurancePeopleService insurancePeopleService;

	public IInsurancePeopleService getPeopleService() {
		return insurancePeopleService;
	}

	@Resource
	public void setPeopleService(IInsurancePeopleService insurancePeopleService) {
		this.insurancePeopleService = insurancePeopleService;
	}

	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "peopleDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";

	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel,
			InsurancePeopleSearch insurancePeopleSearch, boolean ajaxPost) {

		pagerModel.setTotalAmount(insurancePeopleService.queryCount(insurancePeopleSearch));
		List<InsurancePeople> insurancePeoples = insurancePeopleService.queryList(insurancePeopleSearch, pagerModel);

		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("insurancePeoples", insurancePeoples);
		model.addAttribute("insurancePeopleSearch", insurancePeopleSearch);

		if (ajaxPost) {
			return view(outputViewName);
		}

		return view(indexViewName);
	}

	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET })
	public String peopleDetailed(Model model, String key) {
		InsurancePeople insurancePeople = new InsurancePeople();
		if (!StringUtils.isEmpty(key)) {
			insurancePeople = insurancePeopleService.queryModel(key);
		}
		model.addAttribute("insurancePeople", insurancePeople);
		return view(detailedViewName);
	}

	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model, InsurancePeople insurancePeople) {
		if (StringUtils.isEmpty(insurancePeople.getId())) {
			insurancePeople.setId(CommonUtils.getKeyId());
			insurancePeople.setCreateDate(DateUtils.getCurrent());
			insurancePeople.setDeleteMark(YesOrNoInteger.no);
			insurancePeople.setCreateManagerName(getLoginModel().getLoginName());
			insurancePeople.setModelStatus(ModelStatus.add);
		} else {
			insurancePeople.setModifyDate(DateUtils.getCurrent());
			insurancePeople.setModifyManagerName(getLoginModel().getLoginName());
			insurancePeople.setModelStatus(ModelStatus.update);
		}
		insurancePeople.setMarkCode(ChineseUtils.getPinYinHeadChar(insurancePeople.getName()));
		insurancePeople.setCode(ChineseUtils.getPingYin(insurancePeople.getName()));

		ActionResult actionResult;
		if (insurancePeopleService.applyChange(insurancePeople)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}

		model.addAttribute("insurancePeople", insurancePeople);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);

		return view(detailedViewName);
	}

	@RequestMapping(value = deleteViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public @ResponseBody String delete(String key) {
		if (insurancePeopleService.deleteFakeByKey(key)) {
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
}